//
//  TankLayer.m
//  lust
//
//  Created by fan on 13-4-1.
//
//

#import "TankLayer.h"
#import "ControllLayer.h"
@implementation TankLayer
@synthesize mapY,mapX,tank,enemyList,screenHeight,screenWidth,gameWorld,tileSize;
+(CCScene*)scene
{
    CCScene* scene= [CCScene node];
    [scene addChild:[TankLayer node]];
    return scene;
}
-(id)init
{
    self =[super init];
    if (self) {
        gameWorld=[CCTMXTiledMap tiledMapWithTMXFile:@"Level1.tmx"];
        [self addChild:gameWorld z:0 tag:9];
        
        CCSprite* spriteInd =[CCSprite spriteWithFile:@"enemy.png" rect:CGRectMake(0, 0, 40, 40)];
        [self addChild:spriteInd z:1 tag:5];
        spriteInd.scale=0.8;
        spriteInd.position=ccp(20, 300);
        
        lbEnemy=[CCLabelBMFont labelWithString:@"00" fntFile:@"font09.fnt"];
        lbEnemy.anchorPoint=ccp(0.0, 1.0);
        lbEnemy.scale=0.8;
        [self addChild:lbEnemy z:1 tag:6];
        lbEnemy.position=ccp(40, 305);
        
        [self schedule:@selector(showState:)interval:0.5];
        
        tank=[TankSprite TankWithinLayer:self imageFile:@"Tank.png"];
        [tank setPosition:ccp(20, 20)];
        tank.bIsEnemy=NO;
        
        enemyList=[[NSMutableArray alloc]initWithCapacity:8];
        TankSprite* enemyOne;
        for(int i=0;i<10;i++)
        {
            enemyOne=[TankSprite TankWithinLayer:self imageFile:@"enemy.png"];
            enemyOne.bIsEnemy=YES;
            [enemyOne setPosition:ccp(i*80-20, 560)];
            [enemyOne Activate];
            [enemyList addObject:enemyOne];
        }
        
        sheetExplode=[CCSpriteBatchNode batchNodeWithFile:@"explode1.png" capacity:10];
        spriteExplode=[CCSprite spriteWithTexture:sheetExplode.texture rect:CGRectMake(0, 0, 23, 23)];
        [sheetExplode addChild:spriteExplode z:1 tag:5];
        spriteExplode.position=ccp(240, 160);
        [spriteExplode setVisible:NO];
        [self addChild:sheetExplode z:2];
        
        sheetExplodeBig=[CCSpriteBatchNode batchNodeWithFile:@"exploBig.png" capacity:15];
        spriteExplodeBig=[CCSprite spriteWithTexture:sheetExplodeBig.texture rect:CGRectMake(0, 0, 40, 40)];
        [sheetExplodeBig addChild:spriteExplodeBig z:1 tag:5];
        spriteExplode.position=ccp(240, 160);
        [spriteExplode setVisible:NO];
        [self addChild:sheetExplodeBig z:2];
       
        [self setIsTouchEnabled:YES];
        [[self camera]eyeX:&viewOrgX eyeY:&viewOrgY eyeZ:&viewOrgZ];
        CGSize size=[[CCDirector sharedDirector]winSize];
        screenWidth=size.width;
        screenHeight=size.height;
        tileSize=gameWorld.tileSize.width;
        mapX=0;
        mapY=0;
        [self ShowExplodeAt:ccp(200, 100)];
        [self ShowBigExplodeAt:ccp(300, 200)];
    }
    return self;
}
-(void)showState:(ccTime)dt
{
    int ne;
    ne=0;
    for (TankSprite*pts in enemyList) {
        if (pts.visible) {
            ne++;
        }
    }
    [lbEnemy setString:[NSString stringWithFormat:@"%2d",ne]];
}
-(unsigned int)TileIdFromPosition:(CGPoint)pos
{
    CGPoint cpt=[self tileCoodinateFromPos:pos];
    CCTMXLayer* ly=[gameWorld layerNamed:@"tile"];
    if (cpt.x<0) {
        return -1;
    }
    if (cpt.y<0) {
        return -1;
    }
    if (cpt.x>=ly.layerSize.width) {
        return -1;
    }
    if (cpt.y>=ly.layerSize.height) {
        return -1;
    }
    return [ly tileGIDAt:cpt];
}


-(CGPoint)tileCoodinateFromPos:(CGPoint)pos
{
    int cox,coy;
    CCTMXLayer* ly=[gameWorld layerNamed:@"tile"];
    if (ly==nil) {
        NSLog(@"layer nont found");
        return ccp(-1, -1);
    }
    CGSize szLayer=[ly layerSize];
    CGSize szTile =[gameWorld tileSize];
    cox=pos.x/szLayer.width;
    coy=szLayer.height-pos.y/szTile.height;
    
}
-(void)DestroyTile:(CGPoint)pos
{
    CGPoint cpt=[self tileCoodinateFromPos:pos];
    CCTMXLayer*ly=[gameWorld layerNamed:@"tile"];
    [ly setTileGID:4 at:cpt];
}

-(void)ShowExplodeAt:(CGPoint)posAt
{
    int nSize;
    nSize=23;
    [spriteExplode setPosition:posAt];
    [spriteExplode setVisible:YES];
    CCSpriteFrame* frame=[CCSpriteFrame frameWithTexture:sheetExplode.texture rect:CGRectMake(0, 0, spriteExplode.contentSize.width, spriteExplode.contentSize.height)];
    NSArray* arr=[NSArray arrayWithObject:frame];
    CCAnimation* animation=[CCAnimation animationWithSpriteFrames:arr delay:0.2f];
    for (int i=0; i<8; i++) {
        [animation addSpriteFrameWithTexture:sheetExplode.texture rect:CGRectMake(i*nSize,0,nSize,nSize)];
    }
    id action=[CCAnimate actionWithAnimation:animation];
    id ac=[CCSequence actions:action,nil];
    [spriteExplode runAction:[CCRepeatForever actionWithAction:ac]];
}

-(void)ShowBigExplodeAt:(CGPoint)posAt
{
    spriteExplodeBig.position=posAt;
    spriteExplodeBig.visible=YES;
    
    CCSpriteFrame* frame=[CCSpriteFrame frameWithTexture:spriteExplodeBig.texture rect:CGRectMake(0, 0, spriteExplodeBig.contentSize.width, spriteExplodeBig.contentSize.height)];
    NSArray* arr=[NSArray arrayWithObject:frame];
    CCAnimation* animation=[CCAnimation animationWithSpriteFrames:arr delay:0.2f];
    for (int i=0;i<8;i++) {
        [animation addSpriteFrameWithTexture:sheetExplodeBig.texture rect:CGRectMake(i*40, 0, 40, 40)];
    }
    id action=[CCAnimate actionWithAnimation:animation];
    id ac=[CCSequence actions:action, nil];
    [spriteExplodeBig runAction:[CCRepeatForever actionWithAction:ac]];
    
}
-(void)registerWithTouchDispatcher
{
    [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
@end
